package com.health.tablebean.webservice;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.text.TextUtils;
import android.util.Log;

import com.health.db.FDbManager;
import com.health.tablebean.ARCHIVE_BASEINFO;
import com.health.tablebean.CD_DIABETESFOLLOWUP;
import com.health.tablebean.CD_DRUGCONDITION;
import com.health.tablebean.JTYSCustomerDoctorCont;
import com.health.tablebean.PharmacyList;
import com.health.tools.GlobalConts;
import com.health.viewscontrols.NovaApplication;
import com.health.viewscontrols.qyinfo.QYInfo;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 用户签约数据上传bean
 */
public class QyinfoUploadBean {
    /**
     * 身份证号
     */
    private String cardid;
    /**
     * 创建机构
     */
    private String createrorg;
    /**
     * 创建人
     */
    private String creater;
    /**
     * 签约开始时间
     */
    private String beginservictime;
    /**
     * 签约时间
     */
    private String qytime;
    /**
     * 备注
     */
    private String beizhu;
    /**
     * 服务结束时间
     */
    private String res2;
    /**
     * 签约医生姓名
     */
    private String doctorname;

//	private String res1	; 	合同号
    /**
     * 医生联系方式
     */
    private String doctorphone;
    /**
     * 服务项目
     */
    private String fuwuxiangmu;

    /**
     * 签约类型
     * 1个人签约
     * 2家庭签约
     */
    private String ishome;
    /**
     * 家庭成员
     */
    private List<FamilyMember> home;
    /**
     * 和户主的关系
     */
    private String relation;
    /**
     * 数据来源	来源（0：随访pad,1 web系统添加，2：用户端APP）
     */
    private String sourceType;
    /**
     * 状态	(0:已签约,1：待签约审核，2：已解约,3:解约审核, 4：已过期）
     */
    private String status;

    /**
     * 家庭签约个人 用 身份证号
     */
    private String  cardNum;

    /**
     * 家庭签约个人 用 res3   [uuid]
     */
    private String  res3;

    public String getRes3() {
        return res3;
    }

    public void setRes3(String res3) {
        this.res3 = res3;
    }

    public String getCardid() {
        return cardid;
    }

    public void setCardid(String cardid) {
        this.cardid = cardid;
    }

    public String getCreaterorg() {
        return createrorg;
    }

    public void setCreaterorg(String createrorg) {
        this.createrorg = createrorg;
    }

    public String getCreater() {
        return creater;
    }

    public void setCreater(String creater) {
        this.creater = creater;
    }

    public String getBeginservictime() {
        return beginservictime;
    }

    public void setBeginservictime(String beginservictime) {
        this.beginservictime = beginservictime;
    }

    public String getQytime() {
        return qytime;
    }

    public void setQytime(String qytime) {
        this.qytime = qytime;
    }

    public String getBeizhu() {
        return beizhu;
    }

    public void setBeizhu(String beizhu) {
        this.beizhu = beizhu;
    }

    public String getRes2() {
        return res2;
    }

    public void setRes2(String res2) {
        this.res2 = res2;
    }

    public String getDoctorname() {
        return doctorname;
    }

    public void setDoctorname(String doctorname) {
        this.doctorname = doctorname;
    }

    public String getDoctorphone() {
        return doctorphone;
    }

    public void setDoctorphone(String doctorphone) {
        this.doctorphone = doctorphone;
    }

    public String getFuwuxiangmu() {
        return fuwuxiangmu;
    }

    public void setFuwuxiangmu(String fuwuxiangmu) {
        this.fuwuxiangmu = fuwuxiangmu;
    }

    public String getIshome() {
        return ishome;
    }

    public void setIshome(String ishome) {
        this.ishome = ishome;
    }

    public List<FamilyMember> getHome() {
        return home;
    }

    public void setHome(List<FamilyMember> home) {
        this.home = home;
    }

    public String getRelation() {
        return relation;
    }

    public void setRelation(String relation) {
        this.relation = relation;
    }

    public String getSourceType() {
        return sourceType;
    }

    public void setSourceType(String sourceType) {
        this.sourceType = sourceType;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }


    public String getCardNum() {
        return cardNum;
    }

    public void setCardNum(String cardNum) {
        this.cardNum = cardNum;
    }

    public static List<QyinfoUploadBean> convertData(Context context) {
        List<JTYSCustomerDoctorCont> datas = FDbManager.getInstance(context).findAllByWhere(JTYSCustomerDoctorCont.class,
                "sign!='1' AND CREATER='" + NovaApplication.getJgrUserLoginName() + "'");
        List<JTYSCustomerDoctorCont> datas1 = FDbManager.getInstance(context).findAllByWhere(JTYSCustomerDoctorCont.class,
                "sign!='1' AND CREATER='" + NovaApplication.getJgrUserno() + "'");
        if(datas1.size()>0){
            datas.addAll(datas1);
        }
        List<QyinfoUploadBean> wbdatas = new ArrayList<QyinfoUploadBean>();
        QyinfoUploadBean wbdata;
        for (JTYSCustomerDoctorCont data : datas) {
            Log.e("JTYSCustomerDoctorCont",data.toString());
            wbdata = new QyinfoUploadBean();


            wbdata.setCardid(TextUtils.isEmpty(data.getCardNum()) ? "" : data.getCardNum().toUpperCase());

            wbdata.setCreaterorg(data.getCreaterorg());

            wbdata.setCreater(data.getCreater());
            wbdata.setBeginservictime(data.getBeginservictime());
            wbdata.setQytime(data.getQysj());
            wbdata.setBeizhu(data.getBz());
            //服务结束时间
            wbdata.setRes2(data.getRes2());

            wbdata.setDoctorname(data.getQyys());

//	private String res1	; 	合同号
            wbdata.setDoctorphone(data.getQyysdh());

            /**
             * 服务项目
             */
            wbdata.setFuwuxiangmu(data.getDetailId());
            /**
             * 签约类型
             */
            wbdata.setIshome(data.getSigntype());


            //家庭成员列表
            List<FamilyMember> familyMembers = new ArrayList<>();
            //个人
            if (QYInfo.SIGNTYPE_PERSONAL.equals(data.getSigntype())) {
                wbdata.setHome(familyMembers);
                wbdata.setRelation("");
            } else if (QYInfo.SIGNTYPE_FAMILY.equals(data.getSigntype())) {
                List<ARCHIVE_BASEINFO> baseinfos = NovaApplication.fdbmanger.findAllByWhere(ARCHIVE_BASEINFO.class,
                        "QYINFOID='" + data.getId() + "'");

                if (baseinfos != null) {
                    if (baseinfos.size() > 0) {
                        FamilyMember member = null;
                        for (ARCHIVE_BASEINFO info : baseinfos) {
                            member = new FamilyMember();
                            member.setCardid(info.getIDCARD());
                            member.setRelation(info.getRELATIONID());
                            familyMembers.add(member);
                        }
                    }
                }
                wbdata.setHome(familyMembers);
                //户主
                wbdata.setRelation("DM01-06_0_2");

            }

            /**
             * 签约类型 个人是需要用到
             */
            wbdata.setCardNum(data.getCardNum());

            wbdata.setRes3(data.getRes3());
            wbdata.setSourceType("0");
            wbdata.setStatus("0");
            Log.e("updata---qyinfoubean",wbdata.toString());
            wbdatas.add(wbdata);
        }

        return wbdatas;
    }


    public static class FamilyMember {
        /**
         * 身份证号
         */
        private String cardid;
        /**
         * 家庭关系
         */
        private String relation;

        public String getCardid() {
            return cardid;
        }

        public void setCardid(String cardid) {
            this.cardid = cardid;
        }

        public String getRelation() {
            return relation;
        }

        public void setRelation(String relation) {
            this.relation = relation;
        }
    }

    @Override
    public String toString() {
        return "QyinfoUploadBean{" +
                "cardid='" + cardid + '\'' +
                ", createrorg='" + createrorg + '\'' +
                ", creater='" + creater + '\'' +
                ", beginservictime='" + beginservictime + '\'' +
                ", qytime='" + qytime + '\'' +
                ", beizhu='" + beizhu + '\'' +
                ", res2='" + res2 + '\'' +
                ", doctorname='" + doctorname + '\'' +
                ", doctorphone='" + doctorphone + '\'' +
                ", fuwuxiangmu='" + fuwuxiangmu + '\'' +
                ", ishome='" + ishome + '\'' +
                ", home=" + home +
                ", relation='" + relation + '\'' +
                ", sourceType='" + sourceType + '\'' +
                ", status='" + status + '\'' +
                ", cardNum='" + cardNum + '\'' +
                '}';
    }
}